<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization');

// 处理预检请求
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    http_response_code(200);
    exit;
}

// 简单的路由系统
$request_uri = $_SERVER['REQUEST_URI'];
$path = parse_url($request_uri, PHP_URL_PATH);
$path = str_replace('/index.php', '', $path); // 移除index.php前缀

// 路由映射
$routes = [
    '/api/menu' => 'api/controllers/MenuController.php',
    '/api/orders' => 'api/controllers/OrderController.php',
    '/api/health' => 'health_check.php'
];

// 匹配路由
$matched = false;
foreach ($routes as $route => $file) {
    if (strpos($path, $route) === 0) {
        if (file_exists($file)) {
            require_once $file;
            $matched = true;
            break;
        }
    }
}

// 如果没有匹配到路由，返回404
if (!$matched) {
    http_response_code(404);
    header('Content-Type: application/json');
    echo json_encode([
        'success' => false,
        'message' => '接口不存在',
        'data' => null
    ]);
    exit;
}
?>